<?php

class table {
	// Class constructor
	public function __construct($table) {
		echo '<table id="'.$table.'">';
	}
	
	// Set <td> header class
	public function setHeaderClass($headerClass) {
		$this->headerClass = $headerClass;
	}
	
	// Set <td> row class
	public function setRowClass($rowClass) {
		$this->rowClass = $rowClass;
	}
	
	// Define table headers
	public function tableHeaders($headers) {
		echo '<tr>';
		foreach($headers as $head => $width) {
			echo "<td class=\"$this->headerClass\" width=\"$width\">$head</td>";
		}
		echo '</tr>';
	}
	
	// Define returned columns from query
	public function setColumns($columns) {
		$this->columns = $columns;
	}
	
	// Run query
	public function doQuery($sql) {
		$result = mysql_query($sql);
		if(!$result) { 
			die('Invalid query: ' . mysql_error());
		}
		while($row = mysql_fetch_assoc($result)) {
			echo '<tr>';
			foreach($this->columns as $column) {
				if(strlen($row[$column]) > 40) {
					$row[$column] = substr_replace(
						$row[$column], '..', 40, (strlen($row[$column])));
				}
				echo "<td class=\"$this->rowClass\">$row[$column]</td>";
			}
		}
	}
	
	// </table>
	public function endTable() {
		echo '</table>'
	}
}

?>